package dao

import (
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
)

type t_user struct {
}
var T_user = t_user{}

func (a *t_user)GetList(where ...string) (list gdb.List,err error) {
	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_user")
	if len(where)==0{
	}else {
		model = model.Where(where[0])
	}
	res,err:=model.All()
	return res.List(),err
}

func (*t_user) IsAdmin(userid interface{}) bool {
	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	user, err := db.Model("t_user").Where("userid=?", userid).One()
	if err != nil {
		g.Log().Line(true).Error(err.Error())
		return false
	}
	if user.Map()["admin"] == 1 {
		return true
	}
	return false
}

func (*t_user) GetUserInfo(userid string) gdb.Record {
	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("v_user")
	model = model.Where("userid=?", userid)
	res, err := model.One()
	if err != nil {
		g.Log().Line(true).Error(err.Error())
		return nil
	}
	return res
}

func (*t_user) GetWxUserInfo(openid string) gdb.Record {
	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("v_user")
	model = model.Where("wx_openid=?", openid)
	res, err := model.One()
	if err != nil {
		g.Log().Line(true).Error(err.Error())
		return nil
	}
	return res
}
